package com.dpxm.zdbdg.charts.shuangseqiu.service;

import com.dpxm.zdbdg.charts.shuangseqiu.dao.ShuangSeQiuDao;
import com.dpxm.zdbdg.charts.shuangseqiu.vo.*;
import com.sun.deploy.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vehiclebase.utils.ToolsUtils;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ShuangSeQiuServiceKE {
    @Autowired
    private ShuangSeQiuDao shuangSeQiuDao;
    /*AB规则*/
    public KeIncidentRes  selectIncidentAB(IncidentReq req){
        KeIncidentRes keIncidentRes = new KeIncidentRes(); //返回对象
        List<String> shaNubm = req.getShaNubm();//选择的号码
        List<String> modes = new ArrayList<>();//总的号码1-33
        //更具选择的号码得到未选择的号码
        for (int i=1; i<=33; i++){ //模拟1-35
            modes.add(String.valueOf(i));
        }
        // 未选择的号码（差集）
        List<String>  wiShaNubm = modes.stream().filter(a->!shaNubm.contains(a)).collect(Collectors.toList());
        //查询出15期的号码 A规则使用
        List<QueryShuangSeQiuVO>  incidentA = shuangSeQiuDao.QueryMessage("15");
        //A规则15期号码的集合
        List<Integer> aList = new ArrayList<>();
        //分别将号码转为List
        if(ToolsUtils.notEmpty(incidentA)){
            incidentA.forEach(a->{
                aList.add(a.getQ_one());
                aList.add(a.getQ_two());
                aList.add(a.getQ_three());
                aList.add(a.getQ_four());
                aList.add(a.getQ_five());
                aList.add(a.getQ_six());
            });
        }
        Map<Integer,Integer> map = new HashMap<>();
        for(Integer str:aList){
            Integer i = 1; //定义一个计数器，用来记录重复数据的个数
            if(map.get(str) != null){
                i=map.get(str)+1;
            }
            map.put(str,i);
        }
        //15期内出现次数在2到4的集合
        List<Integer> hotNumb = new ArrayList<>();
        //10期内出现次数1的集合
        List<Integer> notHotNumb = new ArrayList<>();

        for (Integer key : map.keySet()) {
            if(map.get(key) >=2 & map.get(key)<=4){
                hotNumb.add(key);
            }
            if(map.get(key) ==1 ){
                notHotNumb.add(key);
            }
        }
        // List<Integer> 转为 List<String>
        List<String> stringListA_san_wu = hotNumb.stream().map(String::valueOf).collect(Collectors.toList());
        List<String> stringListA_er_si = notHotNumb.stream().map(String::valueOf).collect(Collectors.toList());
        //List交集算出杀的号码中有的号码（）2到4
        List<String> intersectionASan = stringListA_san_wu.stream().filter(shaNubm::contains).collect(Collectors.toList());
        //List交集算出杀的号码中有的号码（）1
        List<String> intersectionArEr = stringListA_er_si.stream().filter(shaNubm::contains).collect(Collectors.toList());
        List<String> incident_A = new ArrayList<>();
        incident_A.addAll(intersectionASan);
        incident_A.addAll(intersectionArEr);
        //去出重复
        List<String> incident_A_Collectors=incident_A.stream().distinct().collect(Collectors.toList());
        //转为List<Integer>
        List<Integer> listNumbeInsr = incident_A_Collectors.stream().map(Integer::parseInt).collect(Collectors.toList());
        //排序
        Collections.sort(listNumbeInsr);
        //转为List<String>
        List<String> stringListA_Qi_Shi_Str = listNumbeInsr.stream().map(String::valueOf).collect(Collectors.toList());

        //最终返回的A
        List<String> listA  =  new ArrayList<>();
        if(ToolsUtils.notEmpty(intersectionASan)){
            if(intersectionASan.size()==1){
                listA.addAll(intersectionASan);
            }
            if(intersectionASan.size()==2){
                listA.addAll(intersectionASan);
            }
            if(intersectionASan.size()>2){

            }

        }
        keIncidentRes.setIncident_A(stringListA_Qi_Shi_Str);
        keIncidentRes.setStr_A_SanWu(intersectionASan);
        keIncidentRes.setStr_A_ErSi(intersectionArEr);

        //查询出18期的号码 B规则使用
        List<QueryShuangSeQiuVO>  incidentB = shuangSeQiuDao.QueryMessage("15");
        //B规则18期号码的集合
        List<Integer> bList = new ArrayList<>();
        if(ToolsUtils.notEmpty(incidentB)){
            incidentB.forEach(a->{
                bList.add(a.getQ_one());
                bList.add(a.getQ_two());
                bList.add(a.getQ_three());
                bList.add(a.getQ_four());
                bList.add(a.getQ_five());
                bList.add(a.getQ_six());
            });
        }
        Map<Integer,Integer> mapShiBa = new HashMap<>();
        for(Integer strs:bList){
            Integer i = 1; //定义一个计数器，用来记录重复数据的个数
            if(mapShiBa.get(strs) != null){
                i=mapShiBa.get(strs)+1;
            }
            mapShiBa.put(strs,i);
        }
        //18期内出现次数在5到7的集合
        List<Integer> ShiBaSan = new ArrayList<>();
        //18期内出现次数2的集合
        List<Integer> ShiBaEr = new ArrayList<>();
        for (Integer keys : mapShiBa.keySet()) {
            if(mapShiBa.get(keys) >=5 & mapShiBa.get(keys)<=7){
                ShiBaSan.add(keys);
            }
            if(mapShiBa.get(keys) ==2){
                ShiBaEr.add(keys);
            }
        }
        // List<Integer> 转为 List<String>
        List<String> stringListB_Qi_Shi = ShiBaSan.stream().map(String::valueOf).collect(Collectors.toList());
        List<String> stringListB_Si_Qi = ShiBaEr.stream().map(String::valueOf).collect(Collectors.toList());

        //List交集算出杀的号码中有的号码（）5到7
        List<String> intersectionBQi = stringListB_Qi_Shi.stream().filter(wiShaNubm::contains).collect(Collectors.toList());
        //List交集算出杀的号码中有的号码（）2
        List<String> intersectionBSi = stringListB_Si_Qi.stream().filter(wiShaNubm::contains).collect(Collectors.toList());
        List<String> incident_B = new ArrayList<>();
        incident_B.addAll(intersectionBQi);
        incident_B.addAll(intersectionBSi);
        //去出重复
        List<String> incident_B_Collectors = incident_B.stream().distinct().collect(Collectors.toList());
        //转为List<Integer>
        List<Integer> listNumbeInsr_b = incident_B_Collectors.stream().map(Integer::parseInt).collect(Collectors.toList());
        //排序
        Collections.sort(listNumbeInsr_b);
        //转为List<String>
        List<String> stringListB_Qi_Shi_Str = listNumbeInsr_b.stream().map(String::valueOf).collect(Collectors.toList());
        keIncidentRes.setIncident_B(stringListB_Qi_Shi_Str);
        keIncidentRes.setStr_B_QiShi(intersectionBQi);
        keIncidentRes.setStr_B_SiQi(intersectionBSi);


        return keIncidentRes;
    }
    public KeIncidentRes  selectIncidentC(IncidentReq req){
        KeIncidentRes keIncidentRes = new KeIncidentRes();
        //查询出25期的开奖号码
        List<QueryShuangSeQiuVO>  incidentC = shuangSeQiuDao.QueryMessage("25");
        List<String> cList = new ArrayList<>();
        //分别将号码转为List
        if(ToolsUtils.notEmpty(incidentC)){
            incidentC.forEach(a->{
                if(ToolsUtils.notEmpty(a.getChongcishu())){
                    if(a.getChongcishu().length()>1){ //已逗号直接拆分成集合
                        List<String> listnChong = Arrays.asList(a.getChongnubm().split(","));
                        cList.addAll(listnChong);
                    }else {
                        cList.add(a.getChongnubm());
                    }
                }
            });
        }
        List<String> incident_C_Collectors = cList.stream().distinct().collect(Collectors.toList());
        //转为List<Integer>
        List<Integer> listNumbeInsr_C = incident_C_Collectors.stream().map(Integer::parseInt).collect(Collectors.toList());
        //排序
        Collections.sort(listNumbeInsr_C);
        //转为List<String>
        List<String> stringListC = listNumbeInsr_C.stream().map(String::valueOf).collect(Collectors.toList());
        keIncidentRes.setIncident_C(stringListC);
        return keIncidentRes;
    }

    public KeIncidentRes  selectIncidentD(IncidentReq req){
        KeIncidentRes keIncidentRes = new KeIncidentRes();

        //查询出15期的开奖号码
        List<QueryShuangSeQiuVO>  incidentD = shuangSeQiuDao.QueryMessage("15");
        List<String> cList = new ArrayList<>();
        //分别将号码转为List
        if(ToolsUtils.notEmpty(incidentD)){
            incidentD.forEach(a->{
                if(ToolsUtils.notEmpty(a.getLianhaonubm())){
                   //已逗号直接拆分成集合
                    List<String> listnChong = Arrays.asList(a.getLianhaonubm().split(","));
                    cList.addAll(listnChong);
                }
            });
        }

        List<String> incident_D_Collectors = cList.stream().distinct().collect(Collectors.toList());
        //转为List<Integer>
        List<Integer> listNumbeInsr_D = incident_D_Collectors.stream().map(Integer::parseInt).collect(Collectors.toList());
        //排序
        Collections.sort(listNumbeInsr_D);
        //转为List<String>
        List<String> stringListD = listNumbeInsr_D.stream().map(String::valueOf).collect(Collectors.toList());
        keIncidentRes.setIncident_D(stringListD);
        return keIncidentRes;

    }

    public KeIncidentRes  selectIncidentE(IncidentReq req){
        KeIncidentRes keIncidentRes = new KeIncidentRes();
        //查询出200期的开奖号码
        List<QueryShuangSeQiuVO>  incidentD = shuangSeQiuDao.QueryMessage("200");
        List<Integer> dList = new ArrayList<>();
        if(ToolsUtils.notEmpty(incidentD)){
            incidentD.forEach(a->{
                dList.add(a.getQ_one());
                dList.add(a.getQ_two());
                dList.add(a.getQ_three());
                dList.add(a.getQ_four());
                dList.add(a.getQ_five());
                dList.add(a.getQ_six());
            });
        }

        Map<Integer,Integer> map = new HashMap<>();
        for(Integer str:dList){
            Integer i = 1; //定义一个计数器，用来记录重复数据的个数
            if(map.get(str) != null){
                i=map.get(str)+1;
            }
            map.put(str,i);
        }
        //通过ArrayList构造函数把map.entrySet()转换成list
        List<Map.Entry<Integer, Integer>> mappingList = new ArrayList<Map.Entry<Integer, Integer>>(map.entrySet());
        //通过比较器进行比较排序
        Collections.sort(mappingList, new Comparator<Map.Entry<Integer, Integer>>(){
            public int compare(Map.Entry<Integer, Integer> mapping1, Map.Entry<Integer, Integer> mapping2){
                return mapping1.getValue().compareTo(mapping2.getValue());
            }
        });
        //最冷的集合
        List<Integer> hotNumb = new ArrayList<>();
        //100最热的七个好
        /*有时间挑出相同次数的再筛选一下*/
        for (int i=0 ;i<2 ; i++){
            hotNumb.add(mappingList.get(i).getKey());
            // System.out.println(mappingList.get(i).getKey()+":"+mappingList.get(i).getValue());
        }

        //排序
        Collections.sort(hotNumb);
        //转为List<String>
        List<String> stringListE = hotNumb.stream().map(String::valueOf).collect(Collectors.toList());
        keIncidentRes.setIncident_E(stringListE);
        return keIncidentRes;

    }

    public KeIncidentRes  selectIncidentABCDE(IncidentReq req){
        KeIncidentRes keIncidentRes = new KeIncidentRes();
        //查询库里有无数据有则不在计算直接取表

        List<String> nubmList=req.getShaNubm().stream().distinct().collect(Collectors.toList());
        StringBuffer nubms= new StringBuffer();
        for (int i=0 ;i<nubmList.size() ; i++){
            String a = "0";
            if(i==nubmList.size()-1){
                if (nubmList.get(i).length()==1){
                    nubms.append(a+nubmList.get(i));
                }else {
                    nubms.append(nubmList.get(i));
                }
            }else{
                if (nubmList.get(i).length()==1){
                    nubms.append(a+nubmList.get(i)+",");
                }else {
                    nubms.append(nubmList.get(i)+",");
                }
            }
        }
        String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        QueryShuangSeQiuLisShiVO str = shuangSeQiuDao.QueryShuangSeQiuLisShi(nubms.toString(),date);

        if (ToolsUtils.isEmpty(str)){
        //最后返回的集合
        List<String> strABCDE =new ArrayList<>();
        //杀的号当中15期以内选出出现次数在2到4的两个号  A
        List<String> str_A_SanWu = req.getStr_A_SanWu();
        if(ToolsUtils.notEmpty(str_A_SanWu) ){
            //机选 在3到5的一个号
            List<String> A_SanWu = new ArrayList<>();
            Random random = new Random();
            int num = random.nextInt(str_A_SanWu.size());
            if(str_A_SanWu.get(num).length()==1){
                A_SanWu.add("0"+str_A_SanWu.get(num));
            }else {
                A_SanWu.add(str_A_SanWu.get(num));
            }
            //再机选 在3到5的一个号
            for (int i=0 ;i<str_A_SanWu.size() ; i++){
                Random randomss = new Random();
                int nums = randomss.nextInt(str_A_SanWu.size());
                String shu=str_A_SanWu.get(nums);
                //判断jxHotNumber是否含有shu
                if (A_SanWu.stream().filter(w->String.valueOf(w.toString()).equals(shu)).findAny().isPresent()){
                    continue;
                }
                if (shu.length()==1){
                    A_SanWu.add("0"+shu);
                }else {
                    A_SanWu.add(shu);
                }
                if(A_SanWu.size()==2){
                    break;  //杀的号当中10期以内选出出现次数在3到5的两个号  随机完毕
                }
            }
            strABCDE.addAll(A_SanWu);
        }

        //杀的号当中115期以内选出出现次数在 1一个号  A
        List<String> str_A_ErSi = req.getStr_A_ErSi();
        if(ToolsUtils.notEmpty(str_A_ErSi)){
            for (int i=0 ;i<50 ; i++){
                Random randomss = new Random();
                int nums = randomss.nextInt(str_A_ErSi.size());
                String shu=str_A_ErSi.get(nums);
                //判断jxHotNumber是否含有shu
                if (strABCDE.stream().filter(w->String.valueOf(w.toString()).equals(shu)).findAny().isPresent()){
                    continue;
                }else {
                    if(shu.length()==1){
                        strABCDE.add("0"+shu);
                    }else{
                        strABCDE.add(shu);
                    }
                    break;
                }
            }
        }

        // 没有杀的号当中以15期作为标准  选出出现次数5到7的两个号 B
        List<String> str_B_QiShi = req.getStr_B_QiShi();
        if(ToolsUtils.notEmpty(str_B_QiShi)){
            List<String> panDuanFale =new ArrayList<>();
            for (int i=0 ;i<50; i++){
                Random randomss = new Random();
                int nums = randomss.nextInt(str_B_QiShi.size());
                String shu=str_B_QiShi.get(nums);
                //判断jxHotNumber是否含有shu
                if (strABCDE.stream().filter(w->String.valueOf(w.toString()).equals(shu)).findAny().isPresent()){
                    continue;
                }else {
                    if(shu.length()==1){
                        strABCDE.add("0"+shu);
                    }else{
                        strABCDE.add(shu);
                    }
                    panDuanFale.add(shu);
                }
                if(panDuanFale.size()==2){
                    break;
                }
            }
        }

        // 没有杀的号当中以15期作为标准  选出出现次数2的一个号  B
        List<String> str_B_SiQi = req.getStr_B_SiQi();
        if(ToolsUtils.notEmpty(str_B_SiQi)){
            for (int i=0 ;i<50 ; i++){
                Random randomss = new Random();
                int nums = randomss.nextInt(str_B_SiQi.size());
                String shu=str_B_SiQi.get(nums);
                //判断jxHotNumber是否含有shu
                if (strABCDE.stream().filter(w->String.valueOf(w.toString()).equals(shu)).findAny().isPresent()){
                    continue;
                }else {
                    if(shu.length()==1){
                        strABCDE.add("0"+shu);
                    }else{
                        strABCDE.add(shu);
                    }
                    break;
                }
            }
        }

        // 斜连号以15期为准 选出次数在3到5次的一个 C  现在有八个号
        List<String> incident_C = req.getIncident_C();
        if(ToolsUtils.notEmpty(incident_C)){
            List<String> panDuanFale =new ArrayList<>();
            for (int i=0 ;i<52; i++){
                Random randomss = new Random();
                int nums = randomss.nextInt(incident_C.size());
                String shu=incident_C.get(nums);
                //判断jxHotNumber是否含有shu
                if (strABCDE.stream().filter(w->String.valueOf(w.toString()).equals(shu)).findAny().isPresent()){
                    continue;
                }else {
                    if(shu.length()==1){
                        strABCDE.add("0"+shu);
                    }else{
                        strABCDE.add(shu);
                    }
                    panDuanFale.add(shu);
                }
                if(panDuanFale.size()==1){
                    break;
                }
            }
        }

        // 斜连号以15期为准 选出次数在2到4次的1个  D 现在有10个号
        List<String> incident_D = req.getIncident_D();
        if(ToolsUtils.notEmpty(incident_D)){
            List<String> panDuanFale =new ArrayList<>();
            for (int i=0 ;i<70; i++){
                Random randomss = new Random();
                int nums = randomss.nextInt(incident_D.size());
                String shu=incident_D.get(nums);
                //判断jxHotNumber是否含有shu
                if (strABCDE.stream().filter(w->String.valueOf(w.toString()).equals(shu)).findAny().isPresent()){
                    continue;
                }else {
                    if(shu.length()==1){
                        strABCDE.add("0"+shu);
                    }else{
                        strABCDE.add(shu);
                    }
                    panDuanFale.add(shu);
                }
                if(panDuanFale.size()==1){
                    break;
                }
            }
        }
        //以200期为准 出现次数最少的2个号  直接加入不用循环
        if(ToolsUtils.notEmpty(req.getIncident_E())){
            for (int i=0 ;i<req.getIncident_E().size(); i++){
                if(req.getIncident_E().get(i).length()==1){
                    strABCDE.add("0"+req.getIncident_E().get(i));
                }else{
                    strABCDE.add(req.getIncident_E().get(i));
                }
            }

        }

        //转为List<Integer>
        List<Integer> listNumbeInsr_ABCDE = strABCDE.stream().map(Integer::parseInt).collect(Collectors.toList());
        //排序
        Collections.sort(listNumbeInsr_ABCDE);
        //转为List<String>
        List<String> stringListABCDE = listNumbeInsr_ABCDE.stream().map(String::valueOf).collect(Collectors.toList());
        List<String> cotList=stringListABCDE.stream().distinct().collect(Collectors.toList());

            StringBuffer jisuanlist= new StringBuffer();
            for (int i=0 ;i<cotList.size() ; i++){
                String a = "0";
                if(i==cotList.size()-1){
                    if (cotList.get(i).length()==1){
                        jisuanlist.append(a+cotList.get(i));
                    }else {
                        jisuanlist.append(cotList.get(i));
                    }
                }else{
                    if (cotList.get(i).length()==1){
                        jisuanlist.append(a+cotList.get(i)+",");
                    }else {
                        jisuanlist.append(cotList.get(i)+",");
                    }
                }
            }
            List<String> split= Arrays.asList(jisuanlist.toString().split(","));
            keIncidentRes.setStr_ABCDE(split);
        QueryShuangSeQiuLisShiVO queryShuangSeQiuLisShiVO = new QueryShuangSeQiuLisShiVO();
        queryShuangSeQiuLisShiVO.setCreatedate(date);
        queryShuangSeQiuLisShiVO.setJisuanlist(jisuanlist.toString());
        queryShuangSeQiuLisShiVO.setNubm(nubms.toString());
        //为空时则说明是改天第一次分析以数据  存表
        shuangSeQiuDao.saveShuangSeQiuLisShi(queryShuangSeQiuLisShiVO);
        return keIncidentRes;
        }else {
            String strnub = str.getJisuanlist();
            List<String> split= Arrays.asList(strnub.split(","));
            keIncidentRes.setStr_ABCDE(split);
            return keIncidentRes;
        }
    }
}
